using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevelData;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace GGJ11
{
    //draw player
    class RenderingPlayer
    {

        Level level;
        Texture2D invulnerability;
        private Animation playerDie;
        private AnimationPlayer play;

        public RenderingPlayer() { }
        public RenderingPlayer(ContentManager Content)
        {
            level = Content.ServiceProvider.GetService(typeof(Level)) as Level;
            playerDie = new Animation(Content.Load<Texture2D>("Sprites/Player/fire"), 0.05f, false);
            play.PlayAnimation(playerDie);
            invulnerability = Content.Load<Texture2D>("Sprites/Player/invulnerability"); ;
        }

        //draw the player
        public void Draw(GameTime gameTime, SpriteBatch sp)
        {
            SpriteEffects flip;
            player player = level.playerLevel;

            if (level.playerLevel.velocity.X > 0)
            {
                flip = SpriteEffects.None;
                level.playerLevel.orientationSx = false;
            }
            else if (level.playerLevel.velocity.X < 0)
            {
                flip = SpriteEffects.FlipHorizontally; //None
                level.playerLevel.orientationSx = true;
            }
            else
            {
                if (level.playerLevel.orientationSx)
                    flip = SpriteEffects.FlipHorizontally;
                else
                    flip = SpriteEffects.None;
            }


            if (!player.onFighting)
            {
                if(flip == SpriteEffects.None)
                    play.Draw(gameTime, sp, player.position + (Vector2.UnitX * player.localBounds.Width), flip);
                else
                    play.Draw(gameTime, sp, player.position - (Vector2.UnitX * player.localBounds.Width), flip);
            }
            else
            {
                play.ResetAnimation(playerDie);
            }

            //effect intermittance when player die
            if (level.playerLevel.dieTime >= 0.4f)
            {
                if (player.dieTime >= 2.50)
                {
                    player.sprite.Draw(gameTime, sp, player.position, flip);
                }
                else if (player.dieTime <= 2.4 && player.dieTime > 2.0)
                {
                    player.sprite.Draw(gameTime, sp, player.position, flip);
                }
                else if (player.dieTime <= 1.9 && player.dieTime > 1.5)
                {
                    player.sprite.Draw(gameTime, sp, player.position, flip);
                }
                else if (player.dieTime <= 1.4 && player.dieTime >= 1)
                {
                    player.sprite.Draw(gameTime, sp, player.position, flip);
                }
                else if (player.dieTime <= 0.9 && player.dieTime >= 0.4)
                {
                    player.sprite.Draw(gameTime, sp, player.position, flip);
                }
            }
            else
            {
                player.sprite.Draw(gameTime, sp, player.position, flip);
            }
            if (player.invulnerability > 0)
            {
                sp.Draw(invulnerability, player.position - (Vector2.UnitX * 10), Color.White);
            }

        }


        //update service when load a next level
        public void UpdateLevelService(ContentManager content)
        {
            level = content.ServiceProvider.GetService(typeof(Level)) as Level;
        }
    }
}
